﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：PriceAdjustPlanAdd.cs
    // 文件功能描述：新增价格调整计划
    // 创建标识：吕海乐2009-04-08
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class PriceAdjustPlanAdd : BasePage
    {
        public PriceAdjustPlanAdd()
        {
            this.ModuleNo = Module.调价计划列表.ToString();
            this.PageOperateType = OperateType.调价计划列表.ToString();
        }
        public static string ProjectID = string.Empty;
        public static string name { get; set; }//通用名
        public static string ComSC { get; set; }//生产企业
        //返回的页面
        public string RetUrl = "";
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取需要新增调价计划的采购产品的信息
            if (!this.IsPostBack)
            {
                if (Request["procureCatalogId"] == null)
                {
                    PageError("参数错误！", this.ReturnUrl);
                    return;
                }
                SetBind();
                
            }
            //取消带企业参数edit by xy20121221
            //RetUrl = string.Format("/Admin/BidProdManage/BidProductList.aspx?name={0}&ComSc={1}", name, ComSC);
            RetUrl = string.Format("/Admin/BidProdManage/BidProductList.aspx?name={0}", name);
        }

        public void SetBind()
        {
            int total = 0;
            string procureCatalogId = Request.QueryString["procureCatalogId"].ToString();
            Model.View.ProcureCatalog procureCatalog = ipb.GetProcureCatalogByID(procureCatalogId);
            Model.AdjustPricePlan adjustPricePlan = ipb.GetAdjustPricePlanByID(procureCatalogId);
            IList<Model.AdjustPricePlan> adjustList = ipb.GetAdjustPricePlanByWhere(procureCatalogId, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out total);
            if (procureCatalog != null)
            {
                txtCompany_SC.Text = procureCatalog.CompanyName_SC;
                txtCompany_TB.Text = procureCatalog.CompanyName_TB;
                txtDateTime.Text = string.Format("{0:d}", procureCatalog.AddTime);
                txtPackUnit.Text = procureCatalog.PackUnit.ToString();
                txtGoodsName.Text = procureCatalog.GoodsName;
                txtOutLook.Text = procureCatalog.Outlookc;
                //txtSortSource.Text = HZ.DrugTrade.Web.Function.FormatDrugSource(procureCatalog.SortSource);
                //txtPack.Text = procureCatalog.Pack;
                //txtProcurePrice.Text = getPrice(procureCatalog.ProcurePrice.ToString());//.Substring(0, procureCatalog.ProcurePrice.ToString().Length - 1);
                txtProcurePrice.Text = HZ.DrugTrade.Web.Function.FormPrice(procureCatalog.ProjectID, procureCatalog.ProcurePrice);
                ProjectID = procureCatalog.ProjectID;
                txtProject.Text = procureCatalog.ProjectName;
                //txtSort.Text = procureCatalog.SortName;
                txtUnit.Text = procureCatalog.Unit;
                lblQualityLevel.Text = procureCatalog.QualityLevel;
                txtExecuteStartTime.Text = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:00:00");

                //添加属性
                txtExecuteStartTime.Attributes.Add("onFocus", "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false,readOnly:true,minDate:'" + DateTime.Now.AddHours(0.5).ToString("yyyy-MM-dd HH:mm:ss") + "'})");
                if (adjustPricePlan != null)
                {
                    lblOldPrice.Text = adjustPricePlan.OriginalPrice.ToString();
                    lblPlanTime.Text = adjustPricePlan.ExecuteStartTime.ToString();
                    txtAdjustPriceReason.Text = adjustPricePlan.AdjustPriceReason;
                }
                else
                {
                    lblOldPrice.Text = "";
                    lblPlanTime.Text = "";
                    txtAdjustPriceReason.Text = "";
                }
                name = "";
                ComSC = "";
                name = this.txtGoodsName.Text;
                ComSC = this.txtCompany_SC.Text;
                this.GVAdjustPlanList.DataSource = adjustList;
                this.GVAdjustPlanList.DataBind();
                this.AspNetPager1.RecordCount = total;
            }
            else
            {
                PageError("采购产品不存在！", this.ReturnUrl);
            }
        }
        /// <summary>
        /// 价格保留两位小数 zdc
        /// </summary>
        /// <param name="price"></param>
        /// <returns></returns>
        public string getPrice(string price)
        {
            if (price.Contains("."))
            {
                if (price.Substring((price.IndexOf(".") + 1), price.Length - (price.IndexOf(".") + 1)).Length == 2)
                {
                    return price;
                }
                else if (price.Substring((price.IndexOf(".") + 1), price.Length - (price.IndexOf(".") + 1)).Length == 1)
                {
                    return price.Substring(0, price.Length - 1);
                }
                else
                {
                    return price.Substring(0, price.Length - 2);
                }
            }
            else
            {
                return price + ".00";
            }
        }
        /// <summary>
        /// 增加价格调整计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (string.IsNullOrEmpty(txtAdjustPrice.Text.Trim()))
                {
                    PageError("调整价格不能为空！", "");
                    return;
                }
                if (this.ddlExecuteWay.SelectedIndex == 0)
                {
                    //立即调整价格
                    ImmediateExecute();
                }
                else
                {
                    //定时执行价格调整
                    ScheduleExecute();
                }
            }
        }

        /// <summary>
        /// 立即调整价格
        /// </summary>
        private void ImmediateExecute()
        {
            //
            AdjustPricePlan plan = new AdjustPricePlan()
            {
                AdjustPrice = new HZ.Common.ConvertHelper(txtAdjustPrice.Text.Trim()).ToDecimal,
                AdjustPriceReason = txtAdjustPriceReason.Text.Trim(),
                OriginalPrice = string.IsNullOrEmpty(txtProcurePrice.Text.Trim()) ? 0 : new HZ.Common.ConvertHelper(txtProcurePrice.Text.Trim()).ToDecimal,
                ExecuteStartTime = DateTime.Now,
                PlanPerson = CurrentAccount.GetUser().UserName,
                PlanTime = DateTime.Now,
                PricePlanSerialID = Guid.NewGuid().ToString(),
                ProcureCatalogID = Request["procureCatalogId"].ToString(),
                ExecuteState = 1,
                ExecuteWay = 0
            };
            int flag = ipb.UpdateProcureCatalog_Price(plan);
            if (flag == 0)
            {
                PageError("修改失败！", "");
            }
            else if (flag == 1)
            {
                PageInfo("修改成功！", RetUrl);
            }
        }

        /// <summary>
        /// 定时执行价格调整
        /// </summary>
        private void ScheduleExecute()
        {
            if (string.IsNullOrEmpty(txtExecuteStartTime.Text.Trim()))
            {
                PageError("执行时间不能为空！", this.ReturnUrl);
                return;
            }

            AdjustPricePlan plan = new AdjustPricePlan()
            {
                AdjustPrice = new HZ.Common.ConvertHelper(txtAdjustPrice.Text.Trim()).ToDecimal,
                AdjustPriceReason = txtAdjustPriceReason.Text.Trim(),
                OriginalPrice = string.IsNullOrEmpty(txtProcurePrice.Text.Trim()) ? 0 : new HZ.Common.ConvertHelper(txtProcurePrice.Text.Trim()).ToDecimal,
                ExecuteStartTime = new HZ.Common.ConvertHelper(txtExecuteStartTime.Text.Trim()).ToDateTime,
                PlanPerson = CurrentAccount.GetUser().UserID,
                PlanTime = DateTime.Now,
                PricePlanSerialID = Guid.NewGuid().ToString(),
                ProcureCatalogID = Request["procureCatalogId"].ToString(),
                ExecuteState = 0,
                ExecuteWay = 1
            };

            //执行方法
            if (ipb.AddAdjustPricePlan(plan) == 1)
            {
                PageInfo("添加成功！", this.ReturnUrl);
            }
            else
            {
                PageError("添加失败！", this.ReturnUrl);
            }
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            SetBind();
        }


    }
}
